package com.alingrad.android.test.cyborghuman;

import javax.microedition.khronos.opengles.GL10;

import com.alingrad.android.froyoengine.Game;
import com.alingrad.android.froyoengine.collisions.Collidable;
import com.alingrad.android.froyoengine.collisions.CollisionMask;
import com.alingrad.android.froyoengine.graphics.FrameByFrameAnimation;
import com.alingrad.android.froyoengine.graphics.FrameByFrameAnimationHandler;
import com.alingrad.android.froyoengine.graphics.renderable.Sprite;
import com.alingrad.android.froyoengine.tree.GameObject;

public class Cyborg extends GameObject implements Collidable {
	private FrameByFrameAnimationHandler mHandler;
	private boolean mOnGround;
	private float mVspeed;
	private CollisionMask mMask;
	
	private static final int ANIM_AIR = 0;
	private static final int ANIM_RUN = 1;
	
	@Override
	public void load(GL10 gl) {
		mHandler = new FrameByFrameAnimationHandler(
				new FrameByFrameAnimation(66, 
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_air01, true)), 
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_air02, true)), 
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_air03, true))), 
				new FrameByFrameAnimation(66, 
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run01, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run02, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run03, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run04, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run05, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run06, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run07, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run08, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run09, true)),
						new Sprite(Game.sResourceLibrary.getOrLoadTexture(gl, R.drawable.zero_run10, true))));
		mHandler.setAnimation(ANIM_RUN);
	}

	public Cyborg() {
		super();
		mX = 100;
		mY = 100;
		mVspeed = 0f;
		mScaleX = 3f;
		mScaleY = 3f;
		mRotation = 0f;
		mMask = new CollisionMask();
	}
	
	@Override
	public void update(long delta) {
		if (!mOnGround) {
			mVspeed -= 2000 * delta / 1000f;
			mY += mVspeed * delta / 1000f;
		}
		if (mY < 100) {
			mY = 100;
			mVspeed = 0;
			mOnGround = true;
		}
		
		final VirtualJoypad joypad = VirtualJoypad.get();
		if (joypad.mAPressed) {
			if (mOnGround) {
				mOnGround = false;
				mVspeed = 1100;
			}
		}
		
		if (mOnGround)
			mHandler.setAnimation(ANIM_RUN);
		else
			mHandler.setAnimation(ANIM_AIR);
		
		mHandler.update(delta);
		mMask.update(mHandler.getCurrentSprite().getCollisionMaskData(), mX, mY, mRotation, 
				mScaleX, mScaleY);
		Game.sRenderManager.scheduleToRender(mHandler.getCurrentSprite()
				, mX, mY, -5f, mRotation, mScaleX, mScaleY);
	}

	@Override
	public boolean isAlive() {
		return true;
	}

	@Override
	public CollisionMask getCollisionMask() {
		return mMask;
	}

}
